package engine.newUi
{
	import engine.control.TurnPapersViewGroup;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.Record;
	import engine.logic.LogSendLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;
	import newx.utils.FilterUtil;
	
	
	public class EliteFBLogic
	{
		public static var _self:EliteFBLogic ; 
		public static function getInstance():EliteFBLogic
		{
			if(_self==null)
			{
				_self = new EliteFBLogic() ; 
			}
			return  _self ; 
			
		}
		public  var _ui:EliteFBUI  ; 
		private var _strObj:Object;
		private var _pageIndex:uint=2 ; 
		private var _model:ModelLocator;
		
		private var _vipLevel:uint ; 
		private var _oldMc:MovieClip;  
		private var _bindManage:BindManage;
		private var _anewTime:uint ; 
		private var _fbCD:uint ; 
		private var _isSweep:Boolean ; //是否开启 扫荡按钮
		private var _indexOneCount:int= -1; 
		private var _indexTwoCount:int= -1;
		private var _indexThreeCount:int= -1;
		private var _indexFourCount:int  = -1 ; 
		
		
		private var _completeFB:Array ;
		private var _pageObj:Object
		
		//
		private var _arrCompletedFB:Array; 
		//
		private var _nowRoleLevel:int ; 
		//当前你所翻的页数
		private var _nowPageIndex:int =2  ;
		//
		private var _SceneMC:MovieClip ; 
		//每一章的 副本数量
		private var _FBCount:uint ; 
		//
		private var _nowFBPage:uint=2 ; 
		public var _windowIsOk:Boolean ;
		private var _snockPage:Boolean = true ; 
		private var _count:int ; 
		//
		private var _TipsIsOk:Boolean; 
		public function EliteFBLogic()
		{
			
		}
		public function Init():void 
		{
			
			
			
			_isSweep =true ; 
			_ui = EliteFBUI.getInstance();
			
			_model= ModelLocator.getInstance();
			
			initShowInfo(); 
			_vipLevel=_model.scene.Role.getVipLevel() ; 
			_bindManage=BindManage.getInstance();
			_ui.fb_gate.alpha = 0.5;
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_UPADTA_CONPLETE_REC),anewSucceed);
			
			
		}
		public function BeforeShow():void
		{
			_windowIsOk= true ;
			bind();
			_completeFB= new Array(); 
			_model.scene.GetPropertiesEx("");
			addListener();
//			if(_pageIndex==0)
//			{
//				
//				_pageIndex=uint(_nowRoleLevel/10);
//				if(_pageIndex>6)
//				{
//					_pageIndex=6; 
//				}
//			}
			_ui.wipeout.visible =false; 
//			judgeFB();
			//-----------------------------------
			
//			if(_nowRoleLevel<30)
//			{
//				
//			}else
//			{
//				
//				if(_nowRoleLevel>=int(_pageIndex*10)+10)
//				{
//					if(_pageIndex==6)
//					{
//						_ui.rightBtn.enabled = false ;
//					}else
//					{
//						_ui.rightBtn.enabled = true ;
//					}
//					
//				}else
//				{
//					_ui.rightBtn.enabled = false ;
//					_ui.leftBtn.enabled = true ;
//				}
//			}
			initScene();
			if(_snockPage)
			{
				getTollGate();
				_snockPage =false ; 
			}else
			{
				judgeFB();
			}
			
			if(_nowFBPage==2)
			{
				_ui.leftBtn.enabled = false ;
				_ui.rightBtn.enabled = false ;
			}else if(_pageIndex==2)
			{
				_ui.leftBtn.enabled = false ;
				
			}else
			{
				_ui.leftBtn.enabled = true ;
			}
			
		}
		/**
		 *初始化场景--------------
		 * 
		 */		
		private function initScene():void
		{
			if(_SceneMC==null)
			{
				SwfLoader.StartLoad(StageInfo.swfResPath+"HeroFB.swf", null, createScene);
				
			}else
			{
				
			}
		}
		private function createScene(mc:MovieClip,pl:Array):void
		{
			if(mc)
			{
				_SceneMC= mc ; 
				_SceneMC.x = 13;  
				
				_SceneMC.y = 84 ;
				_ui.windowBG.addChildAt(_SceneMC,_ui.windowBG.getChildIndex(_ui.fb_gate));
				
			}
		}
		private function bind():void
		{
			if(_indexOneCount==-1)
			{
				_indexOneCount = _bindManage.BindProp(_model.scene.Role,"VIPLevel",this,"levelChange");
				
			}
			if(_indexTwoCount==-1)
			{
				_indexOneCount = _bindManage.BindProp(_model.scene.Role,"FBResetNums",this,"getFBResetNums");
			}
			if(_indexThreeCount==-1)
			{
				_indexThreeCount=_bindManage.BindProp(_model.scene.Role,"Level",this,"getRoleLevel");
			}
			if(_indexFourCount ==-1 )
			{
				_indexFourCount=_bindManage.BindProp(_model.scene.Role,"FBResetStatus",this,"getFBResetStatus");
			}
		}
		private function unbind():void
		{
			if(_indexOneCount!=-1)
			{
				_indexOneCount=_bindManage.UnbindProp(_model.scene.Role,"VIPLevel",_indexOneCount);
			}
			if(_indexTwoCount!=-1)
			{
				_indexTwoCount=_bindManage.UnbindProp(_model.scene.Role,"FBResetNums",_indexTwoCount);
			}
			if(_indexThreeCount!=-1)
			{
				_indexThreeCount=_bindManage.UnbindProp(_model.scene.Role,"Level",_indexThreeCount);
			}
			if(_indexFourCount!=-1)
			{
				_indexFourCount=_bindManage.UnbindProp(_model.scene.Role,"FBResetStatus",_indexFourCount);
			}
		}
		
		public function AfterHide():void
		{
			
			if(_oldMc!=null)
			{
				_oldMc.gotoAndStop(1);
				_oldMc["enterBtn"].visible= false ; 
				_oldMc=null; 
			}
			unbind();
			clearImage();
		}
		private function addListener():void
		{
			_ui.fb_frame.addItemsEvent(MouseEvent.CLICK,itemClickHandle);
			_ui.anewBtn.addEventListener(MouseEvent.CLICK,anewHanler);  
			_ui.wipeout.addEventListener(MouseEvent.CLICK,wipeoutHandler);
			_ui.rightBtn.addEventListener(MouseEvent.CLICK,rightHandler); 
			_ui.leftBtn.addEventListener(MouseEvent.CLICK,leftHandler); 
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,closeHandler);
			
			
			_ui.fb_frame.addItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.fb_frame.addItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
			
		}
		private function removeListener():void
		{
			_ui.rightBtn.removeEventListener(MouseEvent.CLICK,rightHandler); 
			_ui.leftBtn.removeEventListener(MouseEvent.CLICK,leftHandler); 
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,closeHandler);
		}
		/**
		 * 精英副本Tips 
		 * @param e 
		 * 
		 */		
		private function onShowTips(e: MouseEvent ):void
		{
			var item:MovieClip	=	e.currentTarget  as MovieClip;
			

				if(item["Tips"]&&item["Click"])
				{
					var tip:String=StaticIni.getSwfLangStr(item["Str_chapterDropDes"]) ;
					TipsMainLogic.instance.showString(tip);
					item.gotoAndStop(2);
					
				}
//			var gold:MovieClip  = item.parent.pic as MovieClip;
//			var str:String= (item.parent.Str_chapterDropDes).toString;
			
//			addGlowListener(item.pic);
		}
		private function onHideTips(e : MouseEvent ):void
		{
			TipsMainLogic.instance.hide();
			var item:MovieClip	=	e.currentTarget  as MovieClip;
			if(item["Click"])
			{
				item.gotoAndStop(1);
			}
			
		}
		/**
		 *	发光 效果 
		 * @param target
		 * 
		 */		
		private function addGlowListener( target : DisplayObject ):void
		{
			target.addEventListener( MouseEvent.MOUSE_OVER, onShowGlow);
			target.addEventListener( MouseEvent.MOUSE_OUT, onHideGlow); 
		}
		/**
		 * 鼠标移 上去骰子 发光
		 * @param e
		 * 
		 */		
		private function onShowGlow(e:MouseEvent):void
		{
			var glow:GlowFilter=new GlowFilter(0xFFFFFF);
			var mc:MovieClip =e.target as MovieClip ;
			mc.filters = [glow] ;
//			_ui.tour_4.filters=[glow];
//			_ui.tour_5.filters=[glow];
//			_ui.tour_6.filters=[glow];
			
		}
		/**
		 *  鼠标移除 出 不发光
		 * @param e
		 * 
		 */		
		private  function onHideGlow(e:MouseEvent):void
		{
			var mc:MovieClip =e.target as MovieClip ;
			mc.filters = null ;
//			_ui.tour_4.filters=null;
//			_ui.tour_5.filters=null;
//			_ui.tour_6.filters=null;
		}
		
		
		
		
		//副本选择框
		private function itemClickHandle(e:MouseEvent):void
		{
			var item:MovieClip	=	e.currentTarget as MovieClip;
			if(_oldMc!=null)
			{
				_oldMc.gotoAndStop(1);
				_oldMc["enterBtn"].visible= false ; 
			}
			restItemClick();
			// 判断开通 还是没有开通
			if(getType(item["preFBID"]))
			{
				
				item["Click"] = false;  
				if(getType(item["data"]))
				{
					if(_fbCD==1)
					{
						item.gotoAndStop(3);
						item["enterBtn"].visible= false; 
						_oldMc=item ; 
					}else 
					{
						item.gotoAndStop(3);
						item["enterBtn"].visible= true; 
						_oldMc=item ; 
						item["enterBtn"].addEventListener(MouseEvent.CLICK,enterBtnHandler);
					}
					
				}else
				{
					item.gotoAndStop(3);
					item["enterBtn"].visible= true; 
					_oldMc=item ; 
					item["enterBtn"].addEventListener(MouseEvent.CLICK,enterBtnHandler);
					
				}
				
			}
			
		}
		private function rightHandler(e:MouseEvent):void
		{
			_ui.leftBtn.enabled =true ; 
			_pageIndex++; 
			if(_pageIndex==_nowFBPage)
			{
				
				_ui.rightBtn.enabled = false;  
			}
			else if(_nowRoleLevel>=70&&_pageIndex==6)
			{
				_ui.rightBtn.enabled = false;  
			}
			
			if(_pageIndex>_nowFBPage)
			{
				_pageIndex =_nowFBPage; 
				return ; 
			}
			leftAndRightBtn();
			
		}
		private function leftHandler(e:MouseEvent):void
		{
			_ui.rightBtn.enabled =true ; 
			_pageIndex-- ; 
			if(_pageIndex<2)
			{
				_pageIndex=2;  
				_ui.leftBtn.enabled =false ;
				return ; 
			}
			if(_pageIndex==2)
			{
				_ui.leftBtn.enabled = false;  
			}
			leftAndRightBtn();
			
		}
		//重复
		private function leftAndRightBtn():void
		{
			while(_ui.titleimage.numChildren>1)
			{
				_ui.titleimage.removeChildAt(1);
			}
			//封成函数
			if(_oldMc!=null)
			{
				_oldMc.gotoAndStop(1);
				_oldMc["enterBtn"].visible= false ; 
				_oldMc=null; 
			}
			_strObj=StaticIni.getIniObj("JY_"+_pageIndex);
			_ui.titleimage.source = "res/png/"+_strObj["actName"] +".png";
			
			judgeFB();
		}
		private function closeHandler(e:NxWindowEvent):void
		{
			_windowIsOk = false ; 
			_ui.hide();
		}
		//重置
		private function anewHanler(e:MouseEvent):void
		{
			SitAlerLogic.getIntatic().setUIinfo("UIStr_1001891",["200"],clickAnewsBtn,_ui.anewBtn);
			LogSendLogic.getInstance().btnClickNumSend(22824);
//			if(SitAlerUI.getInstance().checkBtn.currentFrame == 1)
//			{
//				SitAlerUI.getInstance().show();
//			}
//			else
//			{
//				clickAnewsBtn();
//			}
			
		}
		//扫荡
		private function wipeoutHandler(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SWEEP_ELITE_FB, int(_pageObj["chapterEnterScene"]));
		}
		//接受副本消息
		private function initShowInfo():void
		{
			var i:uint = 0 ; 
			for(i; i < 12 ; i++)
			{
				var item:MovieClip = _ui.fb_frame.getItemByIndex( i ); 
				var title:TextField  = item.getChildByName("title") as TextField;
				var pic:MovieClip	=	item.getChildByName("pic") as MovieClip;
				var image:NxClip  = new NxClip () ; 
				image.isCache			=	true;
				var enterBtn:NxButton = new NxButton (); 
				enterBtn.label = StaticIni.getSwfLangStr("CastleArea_ItemOneBtnThree_1") ;
				enterBtn.skin = "Button_2" ; 
				enterBtn.width =65; 
				enterBtn.height = 20; 
				enterBtn.x	=	45;
				enterBtn.y	=	107;
				var  j :uint = 0 ; 
				for(j;j<5 ; j++)
				{
					var star:MovieClip	=	item.getChildByName("s" + j) as MovieClip;
					star.gotoAndStop(1);
				}
				pic.addChild(image); 
				title.htmlText = "";
				item["enterBtn"] = enterBtn ; 
				item["image"]=image;
				item["enterBtn"].visible=false;  
				item.addChild(enterBtn) ; 
			}
			
		}
		
		/**
		 * 
		 * @param arrFB显示副本
		 * 
		 */		
		private function showInfo(arrFB:Array):void
		{
			var i:uint = 0;  
			var _obj:Object;
			for(i;  i <_ui.fb_frame.col*_ui.fb_frame.row;i++)
			{
				var item:MovieClip = _ui.fb_frame.getItemByIndex( i );
				if(i>=_FBCount)
				{
					item.visible = false ; 
				}else
				{
					item.visible = true ; 
					//显示不显示 --------------------
					_obj=StaticIni.getIniObj(arrFB[i].toString());
					if(_obj!=null)
					{
						if(i>0)
						{
							item["preFBID"] = StaticIni.getIniObj(arrFB[i-1].toString())["chapterEnterScene"];
						}else
						{
							item["preFBID"] = 0;
						}
						
					}
					item["Click"] = true;  
					
					item["Str_chapterDropDes"] = _obj["chapterDropDes"];
					
					item["data"]= _obj["chapterEnterScene"];
					//任务限制 ID
					item["taskID"] = _obj["TaskCompleteID"]
					
					if(getType(item["preFBID"]))
					{
						item["Tips"] = true ;
						var j:uint = 0 ; 
						for(j;j<uint(_obj["chapterGrade"]); j++)
						{
							
							var star:MovieClip	=	item.getChildByName("s" + j) as MovieClip; 
							star.visible =false ; 
							star.filters = [] ; 
							star.gotoAndStop(2);
						}
						item["picture"] = _obj["chapterBackPic"];
						item["title"].htmlText =StaticIni.getSwfLangStr(_obj["chapterName"] );
						//任务限制
						if(getType(item["data"])&&DuplicatesLogic.getInstance().getTaskComplete(item["taskID"]))
						{
							if(_fbCD==1)
							{
								item["image"].source ="res/png/"+_obj["chapterMiniPic"]+".png" ;
								//黑白过滤效果
								item["image"].filters=[FilterUtil.GrayColorFilter]; 
								var  _j:uint =0  ; 
								for(_j;_j<uint(_obj["chapterGrade"]); _j++)
								{
									var _star:MovieClip	=	item.getChildByName("s" +_j) as MovieClip; 
									_star.filters=[FilterUtil.GrayColorFilter]
									_star.gotoAndStop(2);
								}
							}else 
							{
								
								item["image"].source ="res/png/"+_obj["chapterMiniPic"]+".png" ; 
								item["image"].filters=[]; 
							}
							
						}
						else
						{
//							_isSweep=false ; 
							//判断是否副本开启
							item["image"].source ="res/png/"+_obj["chapterMiniPic"]+".png" ; 
							item["image"].filters=[]; 
						}
						
						
					}else
					{
						item["image"].source="res/png/" + "FBListSceneS_0000.png";
						item["Tips"] = false ; 
						item["image"].filters=[]; 
						
					}

				}
			}
		}
		public function getCurrFbID():String
		{
			return _pageObj["actName"];
		}
		private function enterBtnHandler(e:MouseEvent):void
		{
			var clickBtn:NxButton	=	e.target as NxButton
			var box:MovieClip	=	clickBtn.parent  as MovieClip;
			
			var sceneID:int	=	int(box["data"]);
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ELITE_FB_ENTER, sceneID);
			 
			var _strObj:Object=StaticIni.getIniObj("JY_"+_pageIndex+"_"+_FBCount);
			if(sceneID==_strObj["chapterEnterScene"])
			{
				_snockPage = true ;
				
			}
			
		}
		private function getInfo():void
		{
			var str:String = _strObj["actName"]
		}
		// 判断你所在的场景有无 对应的经营副本---牛家村和苏州没有
		/**
		 * 初始副本条件 
		 * 
		 */		
		private function judgeFB():void
		{
			wipeOueShow();
			clearImage();
			var i:uint =  1; 
			_strObj=StaticIni.getIniObj("JY_"+_pageIndex);
			_ui.titleimage.source ="res/png/"+_strObj["actName"] +".png" ;
			_FBCount=_strObj["countChapter"]
			//
			var arrFBInfo:Array = new Array();
			for(i;i<=_FBCount;i++)
			{
				if(i==1)
				{
					_pageObj= StaticIni.getIniObj("JY_"+_pageIndex+"_"+i);
				}
				arrFBInfo.push("JY_"+_pageIndex+"_"+i); 
				
			}
			showInfo(arrFBInfo);
			
		}
		/**
		 *每一张 是否有可扫荡的 副本 
		 * 
		 */		
		private function wipeOueShow():void
		{
			var _strObj:Object=StaticIni.getIniObj("JY_"+_pageIndex);
			var _FBCount:int =_strObj["countChapter"];
			var _fbID:int  ; 
			var i:uint = 0 ; 
			var _isok:Boolean = false ;
			;
			for(i;i<_FBCount;i++)
			{
				var _fbIdObj:Object = StaticIni.getIniObj("JY_"+_pageIndex+"_"+int(i+1));
				_fbID = _fbIdObj["chapterEnterScene"];
				for(var j:uint = 0; j<completeFB.length; j++)
				{
					
					if (completeFB[j][0] == _fbID&&completeFB[j][1]!=1)
					{
						_isok= true ; 
						
					}
				}
			}
			
			_ui.wipeout.visible = _isok ; 
			
		}
		/**
		 * 
		 * 
		 */		
		private function clearImage():void
		{
			var i:uint = 0 ; 
			for(i;  i <_ui.fb_frame.col*_ui.fb_frame.row;i++)
			{
				var item:MovieClip = _ui.fb_frame.getItemByIndex( i ); 
				item["image"].source ="";
				item["title"].htmlText = "" ; 
				var j:uint = 0 ; 
				for(j;j<5; j++)
				{
					var star:MovieClip	=	item.getChildByName("s" + j) as MovieClip;
					star.visible =false ; 
					star.gotoAndStop(1);
				}
			}
		}
		/**
		 * 状态是否通过次节
		 * @return 0 是通关了没打过
		 */
 		private function getType(fbID:int):Boolean
		{
			var _isOK:Boolean	=	false;
			var itemArray:Array	= [];
			
			if (fbID == 0) 
			{
				return true;
				
			}
			for (var i:int = 0; i < completeFB.length; i++) 
			{
				itemArray	=	completeFB[i];
				if (itemArray[0] == fbID)
				{
					_isOK	=	true;
					_fbCD = itemArray[1];
					return _isOK ; 
				}
			}
			return _isOK;
		}
		/**
		 * 
		 * @return  判断是否通关次章节的副本 是否要开启下一张副本
		 * 
		 */		
		private function  getTollGate():void
		{
			
				for (var i:int = 0; i < completeFB.length; i++) 
				{
					
					var _strObj:Object=StaticIni.getIniObj("JY_"+_pageIndex)
					var _FBCount:int=_strObj["countChapter"]
					_strObj=StaticIni.getIniObj("JY_"+_pageIndex+"_"+_FBCount);
					var fbID:int= _strObj["chapterEnterScene"] ; 
					var taskID:int =_strObj["TaskCompleteID"];
					//
					if (completeFB[i][0] == fbID&&DuplicatesLogic.getInstance().getTaskComplete(taskID))
					{
						
						_pageIndex++;
						
						_nowFBPage++;
						if(_nowFBPage>6)
						{
							_nowFBPage = 6;
							
						}
						if(_pageIndex>6)
						{
							_pageIndex=   6; 
						}
						
					}
				}
				_ui.rightBtn.enabled = false; 
			 
			 leftAndRightBtn();
		}
		/**
		 * 获取表
		 */
		private function get completeFB():Array
		{
			return (_model.scene.GetPropertiesEx("CompletedFB") as Record).recArr;
		}
		public function set getRoleLevel (value:int):void 
		{
			_nowRoleLevel= value; 
			
			_nowPageIndex = uint(_nowRoleLevel/10);
			if(_nowPageIndex>6)
			{
				_nowPageIndex = 6 ;
			}
			//			if(value>20)
			//			{
			//				_pageIndex= uint(value/10);
			//			}else
			//			{
			//				_pageIndex=2 ; 
			//			}
			
		}
		/**
		 * 重置 更新
		 */
		private function   anewSucceed(e:CustomMsgEvent):void
		{
			judgeFB();
		}
		public function set levelChange(value:int):void
		{
			_vipLevel=value; 
			if(_vipLevel<3)
			{ 
				_ui.anewBtn.visible = false ; 
				
			}else
			{
				_ui.anewBtn.visible = true ;
			}
		}
		public function set getFBResetNums(value:int):void
		{
			_anewTime=value;
			
		}
		private function clickAnewsBtn():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REST_ELITE_FB_COUNT);
		}

 		public function set  getFBResetStatus(value:int):void
		{
			_count= value ; 
			if(_vipLevel>=3)
			{
				if(_count==0)
				{
					_ui.anewBtn.visible = true ; 
				}else
				{
					_ui.anewBtn.visible = false ; 
				}
			}
		
		}
		/**
		 *重置点击属性 
		 * 
		 */		
		private function restItemClick():void
		{
			var i:uint = 0;  
			for(i;  i <_ui.fb_frame.col*_ui.fb_frame.row;i++)
			{
				var item:MovieClip = _ui.fb_frame.getItemByIndex( i );
				item["Click"] = true;  
			}
			
		}
		
	}
}